package com.beimihu.leetcode.哈希表;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

public class HappyNum {
    public boolean isHappy(int n) {
        Set<Integer> hashMap = new HashSet<>();

        while( n != 1 && hashMap.contains(n)){
            hashMap.add(n);
            n =  next(n);
        }
        return n == 1;
    }

    public int next(int n ){
        int totalNum = 0;

        while(n > 0){
            int d = n % 10;
            n = n / 10 ;
            totalNum = d * d + totalNum;
        }

        return totalNum;
    }
}
